package cn.zhx.xa.bi.controller;

import cn.hutool.json.JSONUtil;
import cn.zhx.xa.bi.service.BiChartService;
import cn.zhx.xa.model.dto.bi.ChartQueryRequest;
import cn.zhx.xa.model.dto.bi.GenChartByAiRequest;
import cn.zhx.xa.model.entity.BiChart;
import cn.zhx.xa.model.vo.bi.BiVo;
import cn.zhx.xa.model.vo.common.Result;
import cn.zhx.xa.model.vo.common.ResultCodeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/api/chart")
@Tag(name = "图表管理", description = "图表管理")
public class ChartController {

    @Resource
    private BiChartService biChartService;


    /**
     * 智能分析生成图表（同步）
     * @param multipartFile
     * @param genChartByAiRequestJson
     * @return
     */
//    @PostMapping("/auth/gen")
//    @Operation(summary = "生成图表（同步）")
//    public Result<BiVo> genChartByAi(@RequestPart("file") MultipartFile multipartFile,
//                                     @RequestParam("genChartByAiRequest") String genChartByAiRequestJson) {
//
//        return Result.build(biChartService.genChartByAi(multipartFile, JSONUtil.toBean(genChartByAiRequestJson, GenChartByAiRequest.class)), ResultCodeEnum.SUCCESS);
//    }

    @PostMapping("/auth/gen/async")
    @Operation(summary = "生成图表（异步）")
    public Result<BiVo> genChartByAiAsync(@RequestPart("file") MultipartFile multipartFile,
                                          @RequestParam("genChartByAiRequest") String genChartByAiRequestJson){
        return Result.build(biChartService.genChartByAiAsync(multipartFile, JSONUtil.toBean(genChartByAiRequestJson, GenChartByAiRequest.class)), ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/auth/gen/asyncMq")
    @Operation(summary = "生成图表（消息队列）")
    public Result<BiVo> getBiResponseAsyncMq(@RequestPart("file") MultipartFile multipartFile,
                                          @RequestParam("genChartByAiRequest") String genChartByAiRequestJson){
        return Result.build(biChartService.getBiResponseAsyncMq(multipartFile, JSONUtil.toBean(genChartByAiRequestJson, GenChartByAiRequest.class)), ResultCodeEnum.SUCCESS);
    }


    /**
     * 用户分页查询图表
     *
     * @param chartQueryRequest
     * @return
     */
    @Operation(summary = "用户分页查询图表")
    @PostMapping(value = "/auth/page/{pageNum}/{pageSize}")
    public Result<BiChart> getChartPage(@RequestBody ChartQueryRequest chartQueryRequest,
                                            @PathVariable(value = "pageNum") Integer pageNum,
                                            @PathVariable(value = "pageSize") Integer pageSize) {
        Page<BiChart> pageInfo = biChartService.page(chartQueryRequest, pageNum, pageSize);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "删除图表")
    @DeleteMapping(value = "/auth/deleteChart/{chartId}")
    public Result<Void> deleteChart(@PathVariable Long chartId){
        return Result.build(biChartService.deleteChartById(chartId),ResultCodeEnum.SUCCESS);
    }

}
